package ar.com.globallogic.promomr.domain;

import java.util.Calendar;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import ar.com.globallogic.promomr.utils.CustomDateDeserializer;
import ar.com.globallogic.promomr.utils.CustomDateSerializer;

@Document(collection="users")
public class User {
	
	@Id
	private String id;
	@Indexed(unique=true)
	private String userName;
	private String password;
	private String firstName;
	private String lastName;
	private Date dateOfBirth;
	private Gender gender;
	@DBRef
	private Folder homeFolder;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@JsonIgnore
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@JsonSerialize(using = CustomDateSerializer.class)
	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	@JsonDeserialize(using = CustomDateDeserializer.class)
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	
	public Gender getGender(){
		return this.gender;
	}
	
	public void setGender(Gender gender){
		this.gender = gender;
	}
	
	public Folder getHomeFolder() {
		return homeFolder;
	}

	public void setHomeFolder(Folder homeFolder) {
		this.homeFolder = homeFolder;
	}

	public boolean isUnderAge(int age){
		Calendar birth = Calendar.getInstance();
		birth.setTime(dateOfBirth);
		
		Calendar today = Calendar.getInstance();
		int userAge = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		
		return userAge < age;
	}

}
